package com.kfzx.www.mycalendardemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MonthAdapter extends RecyclerView.Adapter<MonthAdapter.ViewHolder> {

    private Context mContext;
    private List<DayBean> mBeanList;

    public MonthAdapter(Context context, List<DayBean> beanList) {
        mContext = context;
        mBeanList = beanList;
    }

    @NonNull
    @Override
    public MonthAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.day_item, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MonthAdapter.ViewHolder viewHolder, int i) {
        final DayBean bean = mBeanList.get(i);
        if (bean.getDay() > 0) {
            viewHolder.mView.setClickable(true);
            viewHolder.mDayTextView.setText("" + bean.getDay());
            if (bean.getMeetingType() == 0 && bean.getAttendanceType() == 0) {
                viewHolder.mTipLinearLayout.setVisibility(View.INVISIBLE);
            } else {
                int attendanceDegree = bean.getAttendanceType();
                int meetingDegree = bean.getMeetingType();
                switch (attendanceDegree) {
                    case 0:
                        viewHolder.mAttendanceTextView.setVisibility(View.GONE);
                        break;
                    case 1:
                        viewHolder.mAttendanceTextView.setVisibility(View.VISIBLE);
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_green));
//                    viewHolder.mAttendanceTextView.setText("假");
                        viewHolder.mAttendanceTextView.setBackground(mContext.getResources().getDrawable(R.drawable.green_circle));
                        break;
                    case 2:
                        viewHolder.mAttendanceTextView.setVisibility(View.VISIBLE);
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_yellow));
//                    viewHolder.mAttendanceTextView.setText("迟");
                        viewHolder.mAttendanceTextView.setBackground(mContext.getResources().getDrawable(R.drawable.yellow_circle));
                        break;
//                    case 3:
//                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_orange));
//                        viewHolder.mAttendanceTextView.setVisibility(View.VISIBLE);
////                    viewHolder.mAttendanceTextView.setText("早");
//                        viewHolder.mAttendanceTextView.setBackground(mContext.getResources().getDrawable(R.drawable.orange_circle));
//                        break;
                    case 3:
                        viewHolder.mAttendanceTextView.setVisibility(View.VISIBLE);
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_red));
//                    viewHolder.mAttendanceTextView.setText("常");
                        viewHolder.mAttendanceTextView.setBackground(mContext.getResources().getDrawable(R.drawable.red_circle));
                        break;
                    default:
                        viewHolder.mAttendanceTextView.setVisibility(View.GONE);
                        break;
                }
                switch (meetingDegree) {
                    case 0:
                        viewHolder.mMeetingTextView.setVisibility(View.GONE);
                        break;
                    case 1:
                        viewHolder.mMeetingTextView.setVisibility(View.VISIBLE);
//                    viewHolder.mMeetingTextView.setText("请");
                        viewHolder.mMeetingTextView.setBackground(mContext.getResources().getDrawable(R.drawable.green_circle));
                        break;
                    case 2:
                        viewHolder.mMeetingTextView.setVisibility(View.VISIBLE);
//                    viewHolder.mMeetingTextView.setText("迟");
                        viewHolder.mMeetingTextView.setBackground(mContext.getResources().getDrawable(R.drawable.yellow_circle));
                        break;
//                    case 3:
//                        viewHolder.mMeetingTextView.setVisibility(View.VISIBLE);
////                    viewHolder.mMeetingTextView.setText("早");
//                        viewHolder.mMeetingTextView.setBackground(mContext.getResources().getDrawable(R.drawable.red_circle));
//                        break;
                    case 3:
                        viewHolder.mMeetingTextView.setVisibility(View.VISIBLE);
                        viewHolder.mMeetingTextView.setBackground(mContext.getResources().getDrawable(R.drawable.red_circle));
                        break;
                    default:
                        viewHolder.mMeetingTextView.setVisibility(View.GONE);
                        break;
                }

                int max = attendanceDegree > meetingDegree ? attendanceDegree : meetingDegree;
                switch (max) {
                    case 0:
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.android_default_text_color));
                        break;
                    case 1:
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_green));
                        break;
                    case 2:
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_yellow));
                        break;
                    case 3:
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.calendar_red));
                        break;

                    default:
                        viewHolder.mDayTextView.setTextColor(mContext.getResources().getColor(R.color.android_default_text_color));
                        break;
                }
            }
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, "" + bean.getDay(), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            viewHolder.mDayTextView.setText("");
            viewHolder.mTipLinearLayout.setVisibility(View.INVISIBLE);
            viewHolder.mView.setClickable(false);
            viewHolder.mView.setFocusable(false);
        }

    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View mView;
        TextView mDayTextView;
        LinearLayout mTipLinearLayout;
        TextView mAttendanceTextView;
        TextView mMeetingTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;
            mDayTextView = (TextView) itemView.findViewById(R.id.day_text_view);
            mTipLinearLayout = (LinearLayout) itemView.findViewById(R.id.tip_linear_layout);
            mAttendanceTextView = (TextView) itemView.findViewById(R.id.attendance_text_view);
            mMeetingTextView = (TextView) itemView.findViewById(R.id.meeting_text_view);
        }
    }
}
